/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package muhnes.sizoo.dal;

import java.util.List;
import javax.annotation.PreDestroy;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import muhnes.sizoo.modelo.Nacional;
import muhnes.sizoo.HibernateUtilZoologia;
/**
 *
 * @author Ricardo Orellana
 */
public class NacionalDaoImpl implements NacionalDao {
    
    Session sesion;
    
    public NacionalDaoImpl() {
        sesion = HibernateUtilZoologia.getSessionFactory().openSession();
    }
    
    @PreDestroy
    public void destroy() {
        sesion.close();
    }
    
    @Override
    public void guardar( Nacional nacional ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.save( nacional );
        transaccion.commit();
    }

    @Override
    public void guardarColeccion( List< Nacional > nacionales ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.saveOrUpdate( nacionales );
        transaccion.commit();
    }

    @Override
    public void actualizar( Nacional nacional ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( nacional );
        transaccion.commit();
    }

    @Override
    public void actualizarColeccion( List< Nacional > nacionales ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( nacionales );
        transaccion.commit();
    }

    @Override
    public void eliminar( Nacional nacional ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( nacional );
        transaccion.commit();
    }

    @Override
    public void eliminarColeccion( List< Nacional > nacionales ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( nacionales );
        transaccion.commit();
    }

    @Override
    public void eliminarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Nacional nacional = buscarPorId( id );
        sesion.delete( nacional );
        transaccion.commit();
    }

    @Override
    public Nacional buscarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Nacional where CodigoRecoleccion = " + id);
        Nacional nacional = (Nacional)consulta.uniqueResult();
        transaccion.commit();
        return nacional;
    }

    @Override
    public List< Nacional > buscarTodos() {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Nacional");
        List< Nacional > nacionales = consulta.list();
        transaccion.commit();
        return nacionales;
    }
   
}
